package com.example.courselab.ui.feature.login

import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.paint
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.courselab.R
import com.example.courselab.ui.theme.GreenJC
import org.koin.androidx.compose.koinViewModel

@Composable
fun LoginScreen(
  onLoginSuccess: () -> Unit,
  viewModel: LoginViewModel = koinViewModel()
) {
  var username by remember { mutableStateOf("admin") }
  var password by remember { mutableStateOf("admin123") }
  val context = LocalContext.current.applicationContext
  val uiState = viewModel.uiState.collectAsState()

  Column(
    modifier = Modifier
      .fillMaxSize()
      .paint(
        painterResource(id = R.drawable.splash),
        contentScale = ContentScale.Crop
      )
      .padding(horizontal = 26.dp, vertical = 140.dp),
    horizontalAlignment = Alignment.CenterHorizontally,
    verticalArrangement = Arrangement.Bottom
  ) {
    OutlinedTextField(
      value = username,
      onValueChange = { username = it },
      label = { Text(text = "用户名") },
      shape = RoundedCornerShape(20.dp),
      colors = TextFieldDefaults.colors(
        focusedLeadingIconColor = GreenJC,
        unfocusedLeadingIconColor = GreenJC,
        focusedLabelColor = GreenJC,
        unfocusedLabelColor = GreenJC,
        focusedContainerColor = Color.White,
        unfocusedContainerColor = Color.White,
        focusedIndicatorColor = GreenJC,
        unfocusedIndicatorColor = GreenJC,
        unfocusedPlaceholderColor = GreenJC,
      ),
      leadingIcon = {
        Icon(imageVector = Icons.Default.Person, contentDescription = "username")
      },
      modifier = Modifier
        .fillMaxWidth()
        .padding(8.dp)
    )

    OutlinedTextField(
      value = password,
      onValueChange = { password = it },
      label = { Text(text = "密码") },
      shape = RoundedCornerShape(20.dp),
      colors = TextFieldDefaults.colors(
        focusedLeadingIconColor = GreenJC,
        unfocusedLeadingIconColor = GreenJC,
        focusedLabelColor = GreenJC,
        unfocusedLabelColor = GreenJC,
        focusedContainerColor = Color.White,
        unfocusedContainerColor = Color.White,
        focusedIndicatorColor = GreenJC,
        unfocusedIndicatorColor = GreenJC,
        unfocusedPlaceholderColor = GreenJC,
      ),
      leadingIcon = {
        Icon(imageVector = Icons.Default.Lock, contentDescription = "password")
      },
      modifier = Modifier
        .fillMaxWidth()
        .padding(8.dp),
      visualTransformation = if (password.length > 4) PasswordVisualTransformation() else VisualTransformation.None
    )

    when (val state = uiState.value) {
      is LoginScreenEvent.Loading -> {
        CircularProgressIndicator(
          modifier = Modifier.padding(16.dp),
          color = GreenJC
        )
      }

      is LoginScreenEvent.Error -> {
        Toast.makeText(context, state.message, Toast.LENGTH_SHORT).show()
      }

      is LoginScreenEvent.Success -> {
        LaunchedEffect(Unit) {
          onLoginSuccess()
          Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show()
        }
      }

      else -> {}
    }

    Button(
      onClick = {
        Log.d("LoginScreen", "username: $username, password: $password")
        viewModel.login(username, password)
      },
      modifier = Modifier
        .fillMaxWidth()
        .padding(top = 18.dp),
      colors = ButtonDefaults.buttonColors(GreenJC),
      contentPadding = PaddingValues(start = 90.dp, end = 90.dp, top = 16.dp, bottom = 16.dp)
    ) {
      Text(text = "登  录", fontSize = 18.sp)
    }
  }
}